import {
	defineStore
} from 'pinia'
import {
	convertToTree,
	flattenTree,
	handleCheckBoxChecked,
	handleSelectedPopup,
	mapToArr,
	selectedTypeNumComputed,
	SELECT_PAGE_TYPE
} from '../../utils/util'


import _ from 'lodash'
import {
	FormDicStoreKey,
} from '../sotreKey';

export const useDictStore = defineStore('useDictStore', {

	state: () => ({
		dicStoreKey: FormDicStoreKey,
		storeKey: '',
		// checkedValue: [],
		pathData: [],
		// 选择的数据类型展示
		// selectedTypeValue: SELECT_PAGE_TYPE,
		// 用于切换时候的key 处理
		key: '',
		// 已选弹窗列表数据
		// selectedMutiplatePopupDataList: {},

		// ------------
		list: [],
		rewriteSourceData: [],
		viewListData: [],
		copyViewListData: [],
	}),
	actions: {
		onRemovePopupListData(index) {
			this.dicStoreKey[this.storeKey].value.splice(index, 1)
		},
		clearDicStoreKeyValue(key, v) {
			this.dicStoreKey[key].label = ''
			this.dicStoreKey[key].value = v || []
		},
		setCheckedValue_CheckBox(data, key) {
			console.log(data, 'data');
			// this.checkedValue = this.checkedValue.contact(val)
			// 多选
			const val = handleCheckBoxChecked(data.value, data.checked, this.dicStoreKey[this.storeKey].value)

			// this.selectedMutiplatePopupDataList = val

			this.setStoreKeyValue(val, null)



		},
		setCheckValue_Radio(data) {
			// this.checkedValue = data
			this.setStoreKeyValue([data], null)
		},
		setStoreKeyValue(data, label, key) {
			const k = key || this.storeKey
			this.dicStoreKey[k].value = data
			this.dicStoreKey[k].label = label || ''
		},


		setInit(data) {
			console.log(data, 'data');
			this.list = data.list
			this.rewriteSourceData = data.rewriteSourceData
			this.viewListData = data.viewListData
			this.copyViewListData = data.copyViewListData
			this.storeKey = data.storeKey
			this.pathData = []

		},
		setPathData(val) {
			console.log(val);
			this.pathData.push(val)

		},
		setViewListData(val) {
			this.key = _.uniqueId('cell_')
			this.viewListData = val
		},
		onPathItem({
			value,
			index
		}) {
			this.pathData.splice(index + 1)
			const list = _.filter(this.copyViewListData, (item) => item._value === value)
			this.setViewListData(list[0].children, )
		},
		onAll() {
			this.setViewListData(this.copyViewListData)
			this.pathData = []
		},
		onChangeKeywords: _.debounce(function(e) {
			const {
				value
			} = e.detail
			if (value) {
				const filters = _.filter(this.rewriteSourceData, (item) => {
					return item._name.indexOf(value) > -1
				})
				console.log(filters, 'filters');
				this.setViewListData(filters)
			} else {
				this.setViewListData(this.copyViewListData)

			}
		}, 500),
		onSelectAll() {
			const values = _.map(this.viewListData, 'value');

			this.setStoreKeyValue(values, null)

		},
		// setSelectedMutiplatePopupDataList(key, index, value) {
		// 	const subList = this.selectedMutiplatePopupDataList[key]
		// 	subList.splice(index, 1)
		// 	// Object.assign(this.selectedMutiplatePopupDataList, {
		// 	// 	[key]: subList
		// 	// })

		// 	// const arr = mapToArr(this.selectedMutiplatePopupDataList)
		// 	// const values = _.map(arr, 'value')
		// 	// this.checkedValue = values
		// 	// this.selectedTypeValue = selectedTypeNumComputed(values, this.copyViewListData)

		// },
		delDicValue(index, key) {
			if (this.dicStoreKey[key].value instanceof Array) {
				this.dicStoreKey[key].value.splice(index, 1)
			}

		},
		// 考虑废弃
		clearDicValue(key) {
			this.dicStoreKey[key] = {
				key,
				value: null,
				label: null,
			}
			// this.checkedValue = null
		},
	},

	getters: {
		checkedValue() {
			return this.dicStoreKey[this.storeKey].value || JSON.stringify({})
		},
		currentValueLabel() {
			if (this.checkedValue instanceof Array) {
				console.log(this.checkedValue);
				if (this.checkedValue.length) {
					return JSON.parse(this.checkedValue[0]).label
				}
				return ''
			} else if (typeof this.checkedValue === 'string') {
				return JSON.parse(this.checkedValue).label
			}
			return ''
		},
		// 平铺
		flattenCopyViewListData() {
			return flattenTree(this.copyViewListData)
		},
		// 获取选中值的集合
		checkedCollection() {
			const n = []
			const the = this
			_.forEach(the.flattenCopyViewListData, function(item) {
				if (_.includes(the.checkedValue, item._value)) {
					n.push(item)
				}
			})
			return n
		},
		selectedTypeValue() {
			return {
				dic: {
					text: '种',
					num: this.dicStoreKey[this.storeKey].value.length
				},
			}
		}


	},
	persist: true,
})